<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
/**
 *  列表页控制器
 */
class ListsController extends Controller{
    	//显示页
    	public function index(){
    		 	$cid = Q('get.cid',0,'intval');
    		 	$tid = Q('get.tid',0,'intval');
    		 	
    		 	$arcModel = new \Common\Model\Article();
    		 	$category = new \Common\Model\Category();
    		 	$arcTagModel = new \Common\Model\ArticleTag();
    		 	$tagModel = new \Common\Model\Tag();
    		 	
    		 	//通过分类查询文章信息
    			if ($cid) {
    				//查出没有被回收的所有文章
    				$data = array();
    				$field = "aid, author,sendtime,imgs,digest,title,click";
    				//总计总数
    				$total = $arcModel->where("category_cid={$cid} AND is_recycle=0")->count();
    				//定制分页样式
    				$page = Page::row(2)->desc(array('pre'=>'← Previous', 'next'=>'Next → '))->make($total);
    				//获取文章数据
    				$articleData = $arcModel->join('category','category_cid','=','cid')->field($field)->where("category_cid={$cid} AND is_recycle=0")->limit(Page::limit())->orderBy('sendtime','DESC')->get();
    				if ($articleData) {
    						foreach ($articleData as  $v) {
    							$v['tag'] = $arcTagModel->join('tag','tag_tid','=','tid')->where(array('article_aid'=>$v['aid']))->lists('tid,tname');
    							$data[] = $v;
    						}
    				}
    				$this->assign('page', $page);
    				$this->assign('total', $total);
    				$this->assign('cname',$category->where(array('cid'=>$cid))->pluck('cname'));
    				$this->assign('data',$data);
    			}
    			
    			//通过标签查询文章
    			if ($tid) {
    				//查询出所有没有被回收的文章
    				//查出所有的文章ID
    				$aids = $arcTagModel->where(array('tag_tid'=>$tid))->lists('article_aid');
    				$data = array();
    				if ($aids) {
    					//查出所有的文章
    					//获取文章总数
    					$total = $arcModel->where("aid IN (" . implode(',', $aids) .")"  . " AND is_recycle=0")->count();
    					//定制分页样式
    					$page = Page::row(2)->desc(array('pre'=>'← Previous', 'next'=>'Next → '))->make($total);
    					//查出文章信息
    					$data = $arcModel->where("aid IN (" . implode(',', $aids) .")"  . " AND is_recycle=0")->limit(Page::limit())->get();
    					//在查出每篇文章对应标签
    					foreach ($data as $k=>$v) {
    							$data[$k]['tag'] = $arcTagModel->join('tag','tag_tid','=','tid')->where(array('article_aid'=>$v['aid']))->lists('tid,tname');
    					}
    					//分配总数和页码
    					$this->assign('total', $total);
    					$this->assign('page',$page);
    				}
    				$this->assign('cname',$tagModel->where(array('tid'=>$tid))->pluck('tname'));
    				$this->assign('data',$data);
    			}
    			$this->display();
    	}
    	
    	/**
    	 * 文章标题搜索
    	 */
   		public function search(){
   				if (IS_GET) {
	   					$keywords = Q('get.keywords');
	   					if (empty($keywords)) {
	   						$this->error('查询信息必须填写！',U('Index/index'));
	   					}
	   					$article = new \Common\Model\Article();
	   					//得到所有的文章ID
	   					$aids = $article->where("title LIKE '%{$keywords}%'")->lists('aid');
	   					
	   					//查出没有被回收的所有文章
	   					$arcModel = new \Common\Model\Article();
	   					$category = new \Common\Model\Category();
	   					$arcTagModel = new \Common\Model\ArticleTag();
	   					//有文章的时候,查出所有没有被回收的文章数据
	   					if ($aids) {
	   						//查询出所有的
	   						//获取文章信息
	   						$data = $arcModel->field('aid,title,click,author,digest,imgs')->where("aid IN(" . implode(',', $aids) .")" . " AND is_recycle=0")->get();
	   						//循环数组，获取标签
	   						foreach ($data as $k=>$v) {
	   							$data[$k]['tag'] = $arcTagModel->join('tag','tag_tid','=','tid')->where(array('article_aid'=>$v['aid']))->lists('tid,tname');
	   						}
	   					}else{
	   						//没有文章的时候
	   						$data = array();
	   						$this->assign('keywords',$keywords);
	   					}
	   					$this->assign('data',$data);
	   					$this->display();
	   			}else{
	   					$this->error('页面不存在',U('Index/index'));
	   			}
   				
   		}
    	
}
